const webpack = require("webpack");
const path = require("path");
const { VueLoaderPlugin } = require("vue-loader");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: "./src/main.js",
  output: {
    filename: "js/[name]-[contenthash]-bundle.js",
    path: path.resolve(__dirname, "dist"), // join和resolve有什么区别？？
    publicPath: "./",
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: "babel-loader",
        exclude: /node_modules/,
      },
      {
        test: /\.js$/,
        use: "eslint-loader",
        enforce: "pre",
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: [
          // "style-loader",
          MiniCssExtractPlugin.loader,
          "css-loader",
        ],
      },
      {
        test: /\.less$/,
        use: [
          // "style-loader",
          MiniCssExtractPlugin.loader,
          "css-loader",
          "less-loader",
        ],
      },
      {
        test: /\.(png|jpg|jpeg)$/,
        use: {
          loader: "url-loader",
          options: {
            esModule: false, // 解决 src="[object Module]" 图片不展示问题
            limit: 3 * 1024,
            name: "images/[name].[hash].[ext]",
          },
        },
      },
      {
        test: /\.vue$/,
        use: ["vue-loader"],
      },
    ],
  },
  plugins: [
    new VueLoaderPlugin(),
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
      title: "webpack&vue",
    }),
    new webpack.DefinePlugin({
      BASE_URL: JSON.stringify("./"), // 需要是一个代码片段
    }),
    new webpack.ProvidePlugin({
      process: "process/browser",
    }),
    new webpack.HotModuleReplacementPlugin(),
    new MiniCssExtractPlugin({
      // 对输出的css文件进行重命名
      filename: "css/[name].bundle.css",
    }),
  ],
};
